import {cdnRequest} from "@/utils/HttpRequest";


export async function findByAtterList() {
  //SwaggerResources


  return cdnRequest<API.BaseArrayDataResult<API.Attribute>>(
    "attr/findByList", {
      method: "GET"
    })

}

export  async function editAttr(params:API.Attribute){
  return cdnRequest<API.BaseResult>("attr/edit",{
    method: "POST",
    data: params
  })
}
export  async function editAttrValue(params:API.AttributeValue){
  return cdnRequest<API.BaseResult>("attr/valueEdit",{
    method: "POST",
    data: params
  })
}


export async function findBySelectList() {
  //SwaggerResources


  return cdnRequest<API.BaseArrayDataResult<API.Attribute>>(
    "attr/findBySelectList", {
      method: "GET"
    })


}
export async function findByValueListByAttrId(attrId:number) {
  //SwaggerResources


  return cdnRequest<API.BaseArrayDataResult<API.AttributeValue>>(
    "attr/valueListByAttrId", {
      method: "GET",
      params: {attrId: attrId}
    })


}


export async function findBySelectValueList(key: string, type: number = 0) {
  console.log("Attribute attrkey1",key);
  //SwaggerResources
  let attrkey = "attrkey" + type + "NUmber" + key;

  return new Promise<API.BaseArrayDataResult<any>>((resolve, reject) => {
    let items = localStorage.getItem(attrkey);
    if (items) {

      let parse = JSON.parse(items);

      resolve(parse)
    } else {
      let url = "attr/findBySelectValueList";
      console.log("Attribute attrkey",key);
      cdnRequest<API.BaseArrayDataResult<any>>(
        url, {
          method: "GET",
          params: {key: key, type: type}
        }).then((res) => {
        localStorage.setItem(attrkey, JSON.stringify(res))
        resolve(res)
      }).catch(e => {
        reject(e)
      })
    }
  })


}

export async function getResult(key: string, type: number = 0) {
  console.log("Attribute attrkey2",key);
  return new Promise<API.AttributeValue[]>((resolve, reject) => {
    findBySelectValueList(key, type).then((result) => {
      resolve(result.data)
    }).catch(e => {
      reject(e)
    })

  });
}


export async function findBySelectValueListPeomise(columns: [any], type: number = 0) {
  for (let i = 0; i < columns.length; i++) {
    let item = columns[i]
    if (item['valueType'] == "select") {
      const valem = await getResult(item['attribute'], type);
      //  console.log(valem)
      item['valueEnum'] = valem
      item['filters'] = true
      item['onFilter'] = true
      columns[i] = item;
    }
  }
  return columns;
}


export async function findBySelectValueFormListPeomise(columns: [any]) {
  for (let i = 0; i < columns.length; i++) {
    let item = columns[i]
    if (item['columns'] != undefined) {
      //  console.log("findBySelectValueFormListPeomise")
      item['columns'] = await findBySelectValueFormListPeomise(item['columns'])
    }
    if (item['valueType'] == "select") {
      const valem = await getResult(item['attribute'], 0);
      console.log(valem)
      item['request'] = getResult(item['attribute'], 1);
      item['valueEnum'] = valem;
      item['options'] = valem;


      columns[i] = item;
    }
  }
  return columns;
}

